package edu.ccut.saturn.component.impl.sql;

import java.util.Map;

import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.utils.TypeValidation;

/**
 * 判断属性值和给定的数值或其它属性值的相等关系。属性值或被比较的数值不能为null。
 * 
 * @author 温建
 * 
 */
public class IsEqualItemInfo extends AbstractDyadicExprSqlItemInfo {

	public IsEqualItemInfo(String prepend, String property,
			String comparedValue, boolean isProperty) {

		super(prepend, property, comparedValue, isProperty);
	}

	@Override
	public String getSubSql(Map<String, Object> param) throws Exception {

		if (this.getProperty() != null && this.getComparedValue() != null) {
			String leftValue = (String) TypeValidation.getValidTypeValue(
					"String", param.get(this.getProperty()));
			String rightValue = null;

			if (isProperty()) {
				rightValue = (String) TypeValidation.getValidTypeValue(
						"String", param.get(this.getComparedValue()));

			} else {
				rightValue = this.getComparedValue();
			}

			if (leftValue != null && rightValue != null) {

				if (leftValue.equals(rightValue)) {
					return parseSql(param);

				} else {
					return " ";
				}
			}
		}
		throw new SaturnComponentException("");
	}

	@Override
	public String getType() {
		return "isEqual";
	}
}
